package io.renren.modules.nuaa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.nuaa.dao.TNewsDao;
import io.renren.modules.nuaa.dto.TNewsDTO;
import io.renren.modules.nuaa.entity.TNewsEntity;
import io.renren.modules.nuaa.service.TNewsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 新闻管理
 *
 * @author whc 15953295779@126.com
 * @since 3.0 2023-10-31
 */
@Service
public class TNewsServiceImpl extends CrudServiceImpl<TNewsDao, TNewsEntity, TNewsDTO> implements TNewsService {

    @Override
    public QueryWrapper<TNewsEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<TNewsEntity> wrapper = new QueryWrapper<>();

        String title = (String) params.get("title");
        String categoryId = (String) params.get("categoryId");
        if(StringUtils.isNotBlank(title)){
            wrapper.like("title",title);
        }
        if(StringUtils.isNotBlank(categoryId)){
            wrapper.eq("category_id",Integer.parseInt(categoryId));
        }

        return wrapper;
    }


    @Override
    public List<TNewsDTO> queryNewsList(Long categoryId) {

        List<TNewsEntity> tNewsEntityList = baseDao.selectList(new QueryWrapper<TNewsEntity>()
                .eq("category_id", categoryId.intValue())
        );

        return ConvertUtils.sourceToTarget(tNewsEntityList, currentDtoClass());

    }

    /**
     * 获取院士列表
     * @return
     */
    @Override
    public List<TNewsDTO> queryExpertList(Long id) {

        List<TNewsEntity> tNewsEntityList = baseDao.selectList(new QueryWrapper<TNewsEntity>()
                .eq("category_id", id.intValue())
                .orderByAsc("sort")
        );
        //教师培训，活动报名，判断时间是否截止
        if(id == 4) {
            Date nowDate = new Date();

            tNewsEntityList.forEach(e ->{
                if(e.getPubDate() != null) {
                   e.setIfCutOff(nowDate.after(e.getPubDate()));
                }
            });

        }

        return ConvertUtils.sourceToTarget(tNewsEntityList, currentDtoClass());
    }
}